package de.bml.versandanzeige;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

public class StandorteAuflistenActivity extends ListActivity {

	public static final int STANDORT_AUSWAEHLEN_REQUEST = 0;

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.standorte_anzeigen, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_neuer_standort:
			Intent intent = new Intent(this, StandortErstellenActivity.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.standorte_liste);

		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Standort gewaehlterStandort = Standort.getAll().get(
						position);
				Intent intent = new Intent(getApplicationContext(),
						StandortAnzeigenActivity.class);
				intent.putExtra(HauptActivity.STANDORT_EXTRA,
						gewaehlterStandort.getId());
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();

		setListAdapter(new ArrayAdapter<Standort>(this,
				android.R.layout.simple_list_item_1, Standort.getAll()));
	}

	public void standortHinzufuegen(View view) {
		Intent intent = new Intent(this, StandortErstellenActivity.class);
		startActivity(intent);
	}
}
